﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Reflection;
using ZXCompany.Models.Entity;
using ZXCompany.Models.Service;
using ZXCompany.DBUtil;
namespace ZXCompany.Controllers
{
    [ValidateInput(false)]
    [HandleError]
    public class LoginController : Controller
    {
        private static readonly log4net.ILog logger = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        ServiceFactory serfac = new ServiceFactory();
        public ActionResult Index()
        {
            Session["UserCode"] = "";
            Session["UserName"] = "";
            ZX_User zxuser = serfac.GetAccount().GetUserByCode("admin");
            if (zxuser==null || zxuser.usercode==null || zxuser.usercode =="")
            {
                zxuser = new ZX_User();
                zxuser.usercode = "admin";
                zxuser.username = "admin";
                zxuser.password = "admin";
                zxuser.post = "0";
                zxuser.status = "0";
                zxuser.remark = "";
                zxuser.addUserID = 0;
                zxuser.addTime = DateTime.Now.ToString();
                zxuser.updateTime = "";
                serfac.GetAccount().InsertUser(zxuser);
            }
            return View();
        }

        /// <summary>
        /// 用户登陆
        /// </summary>
        /// <returns></returns>
        //[HttpPost]
        public ActionResult Login_Check()
        {
            string ucode = Request.Form["usercode"] == null ? "" : Request.Form["usercode"].ToString();
            string password = Request.Form["password"] == null ? "" : Request.Form["password"].ToString();
            if (ucode=="" || password=="")
            {
                Response.Write("<script>alert('请先输入用户名和密码！');</script>");
                return RedirectToAction("../Login/Index");
                //return View("Index");
            }
            else
            {
                ZX_User user = serfac.GetAccount().GetUserLogin(ucode, password);
                if (user.ID==null || user.ID == 0)
                {
                    Response.Write("<script>alert('用户名或密码输入错误！');</script>");
                    return RedirectToAction("../Login/Index");
                }
                else
                {
                    AccountController ac = new AccountController();
                    //return View(ZXCompany.DBUtil.VirtualPath.getVirtualPath() + "/Account/HomePageImage");
                    //ZX_WholeInfo wholeinfo = serfac.GetAccount().WholeInfoSelect();
                    //return View("../Account/HomePageImage", wholeinfo);
                    //return ac.HomePageImage();
                    Session["UserCode"] = user.usercode;
                    Session["UserName"] = user.username;
                    return RedirectToAction("../Account/HomePageImage");
                }
            }
            //SerFac.GetHomePageImageServie().SaveHomePageImage(HomePI);
            //return Index();
        }

        /// <summary>
        /// 退出系统
        /// </summary>
        /// <returns></returns>
        public ActionResult loginOut()
        {
            string contentString = string.Empty;
            try
            {
                Session["UserCode"] = "";
                Session["UserName"] = "";
                contentString = "<script>window.location.href='" +  VirtualPath.getVirtualPath() + "/login/index.aspx';</script>";
            }
            catch (Exception ex)
            {
                logger.Error(ex.Message);
                return View("Error");
            }
            return Content(contentString);
        }
    }
}
